function Connectivity = connectivityAnalysis(celltypes)
% CONNECTIVITY = CONNECTIVITYANALYSIS(CELLTYPES) analyze the connectivity
% between cells. CELLTYPES is expected to be a matrix whose 4th and 5th
% columns are distance[um] and connectivity(0 or 1). Use xlsread to get the
% connectivity data. eg:sp2sp =
% xlsread('C:\Data\Taro\CELLS\experimentsummary.xlsx','cellpairs','A5:F282');
% witten by Taro Kiritani, tarokiritani2008@u.northwestern.edu on 11/29/2010

celltypes = celltypes(:,4:5);
ConnectedPairs = celltypes(find(celltypes(:,2) == 1),:);
UnconnectedPairs = celltypes(find(celltypes(:,2) == 0),:);
    
% plot the histogram.
figure;
binVector = [0:30:30*ceil(max(celltypes(:,1))/15)];
histoConnected = histc(ConnectedPairs(:,1),binVector);
histoUnconnected = histc(UnconnectedPairs(:,1),binVector);
bar(binVector+15,[histoConnected,histoUnconnected],'group')
xlim([-20 max(celltypes(:,1))]+20)
legend('Connected','Unconnected')
% summarize the distribution.
Connectivity.meanDistConnect = nanmean(ConnectedPairs(:,1));
Connectivity.meanDistUnonnect = nanmean(UnconnectedPairs(:,1));
Connectivity.meanDistance = nanmean(celltypes(:,1));

Connectivity.stdDistConnect = std(ConnectedPairs(:,1));
Connectivity.stdDistUnconnect = std(UnconnectedPairs(:,1));

Connectivity.medDistConnect = median(ConnectedPairs(:,1));
Connectivity.medDistUnconnect = median(UnconnectedPairs(:,1));
    
Connectivity.numConnect = size(ConnectedPairs,1);
Connectivity.numUnconnect = size(UnconnectedPairs,1);
Connectivity.connectRate = Connectivity.numConnect/(Connectivity.numConnect + Connectivity.numUnconnect);

end